from dataclasses import dataclass
from typing import NamedTuple

from collections import namedtuple
from __future__ import annotations
# 据类(或命名元组)给我们带来的好处之一是值相等，这是一种奇特的说法，“两行有相同的序、 sku 和 qty 是相等的。”

@dataclass(frozen=True)
class OrderLine:
    order_id : int
    name: str
    count: int


class Student(NamedTuple):
    id: int
    name: str


Line = namedtuple('Line',['id','name'])


class Normal:
    pass


a = OrderLine(1,'椅子',3)
b = OrderLine(1,'椅子',3)

#True
assert a==b
c = Normal()
d = Normal()

# False
assert c != d
assert Line(1,'aa') == Line(1,'aa')
assert Student(1,'caoyx') == Student(1,'caoyx')


#--值计算

@dataclass(frozen=True)
class Money:
    currency: str
    value: float

    #from __future__ import annotations  导入这句才不会导致下面函数返回类型语法提示 NameError: name 'Money' is not defined
    def __add__(self, other) -> Money:
        if self.currency != other.currency:
            raise ValueError(f"can't add {self.currency} to {other.currency}")
        else:
            return Money(self.currency,self.value+other.value)


print(Money('rmb', 5) + Money('rmb', 7))